//
//  ViewController.m
//  Find the difference
//
//  Created by administrator on 16/8/12.
//  Copyright © 2016年 duanzhenglong. All rights reserved.
//

#import "ViewController.h"
#import "Register(image).h"
#import "GameBegain.h"
#import "SldeSlipMenu.h"
#import "Menu.h"
#import "Game.h"
#import "AppDelegate.h"
@interface ViewController ()<Register_image_Delegate>
@property (weak, nonatomic) IBOutlet UITextField *UserNameText;
@property (weak, nonatomic) IBOutlet UITextField *PassWordText;
- (IBAction)LoginBtn:(id)sender;
- (IBAction)RegisterBtn:(UIButton *)sender;
@property(nonatomic,strong)NSMutableDictionary*DataArray;
@property(nonatomic,strong)NSMutableDictionary*Info;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //创建沙盒将plist内容存放在沙盒中
    if (self.Info==nil) {
        NSString*Path=[NSString stringWithFormat:@"%@/Documents/userList.plist",NSHomeDirectory()];
        self.Info=[NSMutableDictionary dictionaryWithContentsOfFile:Path];
        self.Info=self.DataArray;
        NSLog(@"self.info%@",self.Info);
        [self.Info writeToFile:Path atomically:YES];
    }
}
-(NSMutableDictionary *)DataArray{
    if (!_DataArray) {
        NSString*path=[[NSBundle mainBundle] pathForResource:@"UserInformation.plist" ofType:nil];
        _DataArray=[NSMutableDictionary dictionaryWithContentsOfFile:path];
    }
    return _DataArray;
}
- (IBAction)LoginBtn:(id)sender {
    if ([self.PassWordText.text isEqualToString:@""]||[self.UserNameText.text isEqualToString:@""]) {
        [self Alert:@"用户名或密码不能为空!"];
    }else{
        if ([self.PassWordText.text isEqualToString:[self.Info objectForKey:self.UserNameText.text]]) {
            GameBegain*G1=[[GameBegain alloc]init];
            AppDelegate*del=[[UIApplication sharedApplication]delegate];
            del.NAME=self.UserNameText.text;
            Menu*M1=[[Menu alloc]init];
            SldeSlipMenu*S1=[[SldeSlipMenu alloc]initWithView:M1 andMainView:G1];
            S1.sideslipTapGes.enabled=YES;
            S1.modalTransitionStyle=UIModalTransitionStyleFlipHorizontal;
            [self presentViewController:S1 animated:YES completion:nil];
        }else{
            [self Alert:@"用户名或密码错误，登陆失败!"];
        }
    }
    
}

- (IBAction)RegisterBtn:(UIButton *)sender {
    Register_image_*R1=[[Register_image_ alloc]init];
    R1.delegate=self;//注意
    [R1 show];
}

//提示框
-(void)Alert:(NSString*)Message{
    UIAlertController*alert=[UIAlertController alertControllerWithTitle:@"提示" message:Message preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction*OK=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alert addAction:OK];
    [self presentViewController:alert animated:YES completion:nil];
}
//注册界面的方法调用
-(void)WithImage:(UIImage *)image andName:(NSString *)name andDescrip:(NSString *)descrip{
    if (!name||!descrip) {
    }else{
        NSString*Path=[NSString stringWithFormat:@"%@/Documents/userList.plist",NSHomeDirectory()];
        self.Info=[NSMutableDictionary dictionaryWithContentsOfFile:Path];
        self.Info[name]=descrip;
        NSLog(@"%@",self.Info);
        [self.Info writeToFile:Path atomically:YES];
    }
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
@end
